/*
 * Copyright (C) 2011-2012 Andrew Wells
 *
 * Licensed under the GNU License, Version 3
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Dungeons & Dragons Copyright (C) Wizards of the Coast. I do not claim
 * the right to any information used from their books or resources.
 */

package com.trace.wells.charactertracker;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.UUID;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class CharacterTracker extends Activity implements Serializable {

	private static final long serialVersionUID = 1L;
	/** Called when the activity is first created. */

	FileHelper fh = new FileHelper();

	public Boolean NEW_FILE;

	private String FILENAME = "data/data/com.trace.wells.charactertracker/characters.txt";

	public Button create_button;

	private ArrayList<Character> characterList = new ArrayList<Character>();
	private static ListViewCustomAdapter adapter;
	private ListView itemListView;
	Hashtable<String, String> table = new Hashtable<String, String>();

	/*
	 * 
	 * Open application: characterTrackerOnCreate:
	 *	Check if file exists 
	 * 
	 */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initMain();

		setupList();
	}

	@Override
	public void onResume() {
		super.onResume();

		characterList = fh.readCharacters(ConstantHelper.FILENAME);

		initMain();

		setupList();
	}

	public void setupList() {
		itemListView = (ListView) findViewById(R.id.list);
		adapter = new ListViewCustomAdapter(this, characterList);
		itemListView.setAdapter(adapter);

		itemListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View item, int position, long id) {
				Character character = (Character) adapter.getItem(position);

				Intent i = new Intent(CharacterTracker.this, BattleScreen.class);

				Bundle extras = new Bundle();
				extras.putSerializable("character", character);

				fh.writeCharacters(ConstantHelper.FILENAME, characterList);

				i.putExtras(extras);
				startActivity(i);
			}
		});
		itemListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

				final int listItem = arg2;

				AlertDialog.Builder dialog = new AlertDialog.Builder(CharacterTracker.this);
				dialog.setMessage("Are you sure you want to delete this character?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						Character ri = (Character) adapter.getItem(listItem);
						UUID ID = ri.getUniqueId();
						dialog.cancel();
						adapter.removeCharacter(ID);

						adapter.notifyDataSetChanged();

						fh.writeCharacters(FILENAME, adapter.getCharacterList());
					}
				}).setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});

				AlertDialog alert = dialog.create();
				alert.show();

				return true;
			}
		});

	}

	private void initMain() {
		create_button = (Button) findViewById(R.id.buttonCreate);

		create_button.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				fh.writeCharacters(ConstantHelper.FILENAME, characterList);
				Intent i = new Intent(CharacterTracker.this, CreateCharacter.class);
				startActivityForResult(i, ConstantHelper.REQUEST_CODE_CREATE_CHARACTER);

			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		setContentView(R.layout.main);
		try {
			if ((requestCode == ConstantHelper.REQUEST_CODE_CREATE_CHARACTER) && (resultCode == Activity.RESULT_OK)) {

				Bundle myResults = data.getExtras();

				Character character = (Character) myResults.get("character");

				characterList.add(character);

				fh.writeCharacters(ConstantHelper.FILENAME, characterList);

				setupList();
			}
			if ((requestCode == ConstantHelper.REQUEST_CODE_BATTLE_SCREEN) && (resultCode == Activity.RESULT_OK)) {
				characterList = fh.readCharacters(ConstantHelper.FILENAME);

				initMain();

				setupList();
			}
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), "Data was not read Successfully", Toast.LENGTH_LONG).show();
		}
	}
}